package com.liip.platform.exception;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @description 
 * @author 罗林
 * @date 2020-03-19 15:57:04
 */
//@ControllerAdvice
public class GlobalDefaultExceptionHandler {
	/**
	 * 默认异常映射,返回JSON格式提示
	 */
	@ExceptionHandler(value = Exception.class)
	@ResponseBody
	public ReturnException defaultExceptionHandler(HttpServletRequest request, Exception e) {
		ReturnException returnException = new ReturnException();
		//处理空指针异常
		if(e instanceof NullPointerException) {
			
			returnException.setCode(1);
			returnException.setMsg(e.getMessage());
			returnException.setUrl(String.valueOf(request.getRequestURL()));
		}
		
		returnException.setCode(-1);
		returnException.setMsg("系统异常！");
		returnException.setUrl(String.valueOf(request.getRequestURL()));
		//未知错误
		return returnException;
	}
	/**
	 * Redis业务异常映射,返回JSON格式提示
	 */
	@ExceptionHandler(value = RedisException.class)
	@ResponseBody
	public ReturnException serviceExceptionHandler(HttpServletRequest request, RedisException e) {
		ReturnException returnException = new ReturnException();
		returnException.setCode(500);
		returnException.setMsg(e.getMessage());
		returnException.setUrl(String.valueOf(request.getRequestURL()));
		return returnException;
	}
}
